<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/13
 * Time: 21:47
 */
namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\models\DwDataJobs;
use app\models\DwDataCity;
use yii\filters\VerbFilter;

class BaseController extends Controller
{
    public $enableCsrfValidation=false;
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        if(isset(Yii::$app->request->queryParams['cityid']) && isset(Yii::$app->request->queryParams['jobid'])){
            $jobId = Yii::$app->request->queryParams['jobid'];
            $cityId = Yii::$app->request->queryParams['cityid'];
            $jobModel = DwDataJobs::findOne($jobId);
            $cityModel = DwDataCity::findOne($cityId);

            $sql = "select t2.city_name,t3.job_name,t1.* from dw_data_job_num_by_day t1 LEFT JOIN dw_data_city t2 on t1.city_id=t2.id LEFT JOIN dw_data_jobs t3 on t1.job_id=t3.id where t1.city_id = '{$cityId}' and t1.job_id = '{$jobId}' order by t1.date_line desc LIMIT 30;";
            $jobsAndCityData = Yii::$app->db->createCommand($sql)->queryAll();

        }else{
            return $this->redirect(['site/index']);
        }
        return $this->render('index', [
            'jobModel'=>$jobModel,
            'cityModel'=>$cityModel,
            'jobsAndCityData'=>$jobsAndCityData
        ]);
    }

    /**
     * 提供具体城市职位的平均工资与招聘量
     */
    public function actionCityandjob(){
        if(isset(Yii::$app->request->queryParams['cityid']) && isset(Yii::$app->request->queryParams['jobid'])){
            $jobId = Yii::$app->request->queryParams['jobid'];
            $cityId = Yii::$app->request->queryParams['cityid'];
            $sql = "select date_line,total,talk,avg from dw_data_job_num_by_day where city_id = '{$cityId}' and job_id = '{$jobId}' ORDER BY date_line desc LIMIT 7";
            $jobsData = Yii::$app->db->createCommand($sql)->queryAll();
            krsort($jobsData);
            $retrunData =array();
            foreach($jobsData as $row){
                $retrunData[]= $row;
            }
            $retrunData = json_encode($retrunData);
            echo $retrunData;
        }else{
            echo '';
        }
    }

    /**
     * 提供具体城市职位的区间量
     */
    public function actionSection()
    {
        if(isset(Yii::$app->request->queryParams['cityid']) && isset(Yii::$app->request->queryParams['jobid'])){
            $jobId = Yii::$app->request->queryParams['jobid'];
            $cityId = Yii::$app->request->queryParams['cityid'];
            $sql = "select * from dw_data_job_num_by_day where city_id = '{$cityId}' and job_id = '{$jobId}' ORDER BY id desc LIMIT 1";
            $jobsData = Yii::$app->db->createCommand($sql)->queryOne();
            if(!$jobsData) return '';
            $retrunData = array();
            $total = $jobsData['talk']+$jobsData['4k']+$jobsData['4k_6k']+$jobsData['6k_8k']+$jobsData['8k_10k']+$jobsData['10k_15k']+$jobsData['15k_20k']+$jobsData['20k_30k']+$jobsData['30k'];

            $retrunData[0]=array('up'=>$total,'down'=>0);
            $retrunData[1]=array('up'=>$jobsData['talk'],'down'=>$total-$jobsData['talk']);
            $retrunData[2]=array('up'=>$jobsData['4k'],'down'=>$retrunData[1]['down']-$jobsData['4k']);
            $retrunData[3]=array('up'=>$jobsData['4k_6k'],'down'=>$retrunData[2]['down']-$jobsData['4k_6k']);
            $retrunData[4]=array('up'=>$jobsData['6k_8k'],'down'=>$retrunData[3]['down']-$jobsData['6k_8k']);
            $retrunData[5]=array('up'=>$jobsData['8k_10k'],'down'=>$retrunData[4]['down']-$jobsData['8k_10k']);
            $retrunData[6]=array('up'=>$jobsData['10k_15k'],'down'=>$retrunData[5]['down']-$jobsData['10k_15k']);
            $retrunData[7]=array('up'=>$jobsData['15k_20k'],'down'=>$retrunData[6]['down']-$jobsData['15k_20k']);
            $retrunData[8]=array('up'=>$jobsData['20k_30k'],'down'=>$retrunData[7]['down']-$jobsData['20k_30k']);
            $retrunData[9]=array('up'=>$jobsData['30k'],'down'=>0);
            //var_dump($retrunData);
            $retrunData = json_encode($retrunData);
            echo $retrunData;
        }else{
            echo '';
        }
    }

    public function actionAvgdesc()
    {
//        $sql = "select sum(total_num) total_num  from t_data_job_list where date_line = (select max(date_line) from  t_data_job_list)";
//
//        $jobsNum = Yii::$app->db->createCommand($sql)->queryScalar();
        return $this->render('avgdesc', [
            //'jobsNum'=>$jobsNum
        ]);
    }

    public function actionAvgdescajax(){

        $sql = "select t2.job_name,t1.* from t_data_job_list t1 left JOIN dw_data_jobs t2 on t1.job_id = t2.id  where date_line = (select max(date_line) from  t_data_job_list) and total_num > '10000' order by avg desc limit 20 ";

        $jobsData = Yii::$app->db->createCommand($sql)->queryAll();
        //var_dump($jobsData);
        krsort($jobsData);
        $retrunData =array();
        foreach($jobsData as $row){
            $retrunData[]= $row;
        }
        //var_dump($jobsData);
        $retrunData = json_encode($retrunData);
        echo $retrunData;
    }

}
